package control.util
{
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import ghostcat.util.display.BitmapUtil;
	
	/**
	 * 用于判断鼠标是否在对象像，支持透明和模糊判断 
	 * @author Lyt
	 */	
	public class CollisionBitmap
	{
		/**
		 * 鼠标大小向四方向找有没有颜色的最大值
		 */		
		public static var SIZE:Point = new Point(30,30);
		public static var bitmap:BitmapData;
		
		protected var rect:Rectangle;
		protected var target:DisplayObject;
		
		public function CollisionBitmap(target:DisplayObject)
		{
			this.target = target;
		}
		
		/**
		 * 更新子选区 
		 */		
		public function refreshRectangle():void
		{
			if(!target || target.width == 0 || target.height == 0) return;
			rect = target.getBounds(target);
			rect.x = Math.floor(rect.x);
			rect.y = Math.floor(rect.y);
			rect.width = Math.floor(rect.width);
			rect.height = Math.floor(rect.height);
		}
		
		public function get checkMouseOn():Boolean
		{
			if(!target) return false;
			//rect = target.getBounds(target);
			if(!rect || !rect.contains(target.mouseX,target.mouseY)) return false;
			var x:int = Math.floor(target.mouseX-rect.x);
			var y:int = Math.floor(target.mouseY-rect.y);
			
			drawDisplayObj();
			var color:uint = bitmap.getPixel32(x,y);
			var r:Boolean = color > 0xf0000000;
			if(r) return true;
			
			var count:int = 0;
			var i:int;
			// 左
			for(i = x - SIZE.x;i <= x ; i++)
			{
				if(i < 0) continue;
				if(bitmap.getPixel32(i,y) > 0xf0000000)
				{
					count++;
					break;
				}
			}
			// 右
			for(i = x + SIZE.x;i >= x ; i--)
			{
				if(i >= rect.width) continue;
				if(bitmap.getPixel32(i,y) > 0xf0000000)
				{
					count++;
					break;
				}
			}
			if(count < 1) return false;
			// 上
			for(i = y - SIZE.y;i <= y ; i++)
			{
				if(i < 0) continue;
				if(bitmap.getPixel32(x,i) > 0xf0000000)
				{
					count++;
					break;
				}
			}
			if(count < 2) return false;
			// 下
			for(i = y + SIZE.y;i >= y ; i--)
			{
				if(i >= rect.height) continue;
				if(bitmap.getPixel32(x,i) > 0xf0000000)
				{
					count++;
					break;
				}
			}
			
			return count > 2;
		}
		
		public function destory():void
		{
			this.target = null;
		}
		
		public function drawDisplayObj():void
		{
			var m:Matrix = new Matrix();
			m.translate(-rect.x,-rect.y);
			if(!bitmap || bitmap.width < rect.width || bitmap.height < rect.height)
			{
				if(bitmap) bitmap.dispose();
				bitmap = new BitmapData(rect.width<<1,rect.height<<1,true,0);
			}
			else
			{
				BitmapUtil.clear(bitmap);
			}
			bitmap.draw(target,m);
		}
		
		/**
		 * 上下翻转 
		 * @param bt
		 * @return 
		 */		
		public static function upanddown(bt:BitmapData):BitmapData {
			var bmd:BitmapData = new BitmapData(bt.width, bt.height, bt.transparent, 0);
			for (var xx:int=0; xx<bt.width; xx++) {
				for (var yy:int=0; yy<bt.height; yy++) {
					bmd.setPixel32(xx, bt.height-yy-1, bt.getPixel32(xx,yy));
				}
			}
			return bmd;
		}
		
		/**
		 * 左右翻转 
		 * @param bt
		 * @return 
		 * 
		 */		
		public static function rightandleft(bt:BitmapData):BitmapData {
			var bmd:BitmapData = new BitmapData(bt.width, bt.height, bt.transparent, 0);
			for (var yy:int=0; yy<bt.height; yy++) {
				for (var xx:int=0; xx<bt.width; xx++) {
					bmd.setPixel32(bt.width-xx-1, yy, bt.getPixel32(xx,yy));
				}
			}
			return bmd;
		}
	}
}